//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Configuration;
using EntLibContrib.Query.Commands;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;

namespace EntLibContrib.Query.Configuration
{
	/// <summary>
	/// Configuration data defining ServiceCustomCommandData.
	/// </summary>
    [ResourceDescription(typeof(DesignResources), "WebServiceCommandDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "WebServiceCommandDataDisplayName")]
	public class WebServiceCommandData : CommandData
	{
        private const string endpointProperty = "endpoint";
        private const string addressProperty = "address";
        private const string serviceProperty = "service";
        private const string methodProperty = "method";

		/// <summary>
		/// Initializes a new instance of the <see cref="CustomCommandData"/> class.
		/// </summary>
		public WebServiceCommandData()
            : this(null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="WebServiceCommandData"/> class.
		/// </summary>
		/// <param name="name">The provider name.</param>
		public WebServiceCommandData(string name)
			: base(name, typeof(WebServiceCommand))
		{
		}

        /// <summary>
        /// Name of the data provider.
        /// </summary>
        [ConfigurationProperty(nameProperty, DefaultValue = "Web Service Command")]
        public override string Name
        {
            get { return base.Name; }
            set { base.Name = value; }
        }

        /// <summary>
		/// Name of the end point to use.
		/// </summary>
		/// <value>The name of the end point.</value>
		[ConfigurationProperty(endpointProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "WebServiceCommandDataEndpointDescription")]
        [ResourceDisplayName(typeof(DesignResources), "WebServiceCommandDataEndpointDisplayName")]
		public string Endpoint
		{
			get { return (string)base[endpointProperty]; }
			set { base[endpointProperty] = value; }
		}

        /// <summary>
        /// Address of the service to use.
        /// </summary>
        /// <value>The address of the service.</value>
        [ConfigurationProperty(addressProperty, IsRequired = false)]
        [ResourceDescription(typeof(DesignResources), "WebServiceCommandDataAddressDescription")]
        [ResourceDisplayName(typeof(DesignResources), "WebServiceCommandDataAddressDisplayName")]
        public string Address
        {
            get { return (string)base[addressProperty]; }
            set { base[addressProperty] = value; }
        }

        /// <summary>
        /// Name of the service to use.
        /// </summary>
        /// <value>The name of the service.</value>
        [ConfigurationProperty(serviceProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "WebServiceCommandDataServiceDescription")]
        [ResourceDisplayName(typeof(DesignResources), "WebServiceCommandDataServiceDisplayName")]
        [System.ComponentModel.Editor(CommonDesignTime.EditorTypes.TypeSelector, CommonDesignTime.EditorTypes.UITypeEditor)]
        public string Service
        {
            get { return (string)base[serviceProperty]; }
            set { base[serviceProperty] = value; }
        }

        /// <summary>
        /// Name of the method to use.
        /// </summary>
        /// <value>The name of the method.</value>
        [ConfigurationProperty(methodProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "WebServiceCommandDataMethodDescription")]
        [ResourceDisplayName(typeof(DesignResources), "WebServiceCommandDataMethodDisplayName")]
        public string Method
        {
            get { return (string)base[methodProperty]; }
            set { base[methodProperty] = value; }
        }


        /// <summary>
        /// A <see cref="TypeRegistration"/> container configuration model for <see cref="XmlFileCommandData"/>.
        /// </summary>
        /// <returns>A <see cref="TypeRegistration"/> for registering a <see cref="XmlFileCommandData"/> in the container.</returns>
        public override TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new WebServiceCommand(
                        Address,
                        Service,
                        Method,
                        Container.ResolvedIfNotNull<IParameterSet>(ParameterSetName)))
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}